package javacommon.authority.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import javacommon.authority.model.Org;
import javacommon.kit.SecureKit;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 * 访问路径 /system/org
 * 
 * @author zhengwei
 * 
 */
public class OrgController extends Controller {
	/**
	 * 列出部门
	 */
	public void list() {
		String para0 = getPara(0);
		//json请求
		if (para0 != null && para0.equals("json")) {
			List<Org> page = Org.dao.findAll();
			setAttr("status", "success");
			setAttr("data", page);
			renderJson();
		} else {//非json请求
			render("orgList.html");
		}
	}

	public void update(){
		setAttr("message", getText("update_success"));
		Org org = getModel(Org.class);
		if(StringUtils.isBlank(org.getStr("pid"))){
			org.set("pid",null);
		}
		if(StringUtils.isBlank(org.getStr("id"))){
			org.set("id",SecureKit.getUUID());
			org.save();
		}else {
			org.update();
		}
		setAttr("status", "success");
		renderJson();
	}
	public void delete(){
		String id=getPara(0);
		Org.dao.deleteById(id);
		setAttr("message", getText("delete_success"));
		setAttr("status", "success");
		renderJson();
	}
	public void tree(){
		String para0 = getPara(0);
		if (para0!=null&&para0.equals("pre")) {
			treePre();
		}
	}

	private void treePre() {
		setAttr("status", "success");
		render("tree2.html");
	}
}
